let typeMap = {
  uk: 'Uk',
  us: 'Us'
}
let proImgs = [
  '/images/book/word-pronounce.png',
  '/images/book/word-pronounce-active.png'
]
Component({
  data: {
    UkImg: proImgs[0],
    UsImg: proImgs[0]
  },
  properties: {
    topic: {
      type: null,
      value: null,
      observer: "__wordObserver"
    }
  },
  created() {
    this.typeMap = {
      uk: 'Uk',
      us: 'Us'
    }
  },
  attached() {
    let self = this;
    this.type = null;
    wx.onBackgroundAudioStop(() => {
      this.setVoiceImg(this.type, false);
    })
  },
  methods: {
    __wordObserver(val) {
    },
    playAudio(type) {
      let self = this;
      let topic = this.getTopic();
      if(this.type !== type) {
        wx.stopBackgroundAudio();
      }
      this.setVoiceImg(this.type, false);
      this.setVoiceImg(type, true);
      wx.playBackgroundAudio({
        dataUrl: topic.domain["audio" + type],
        complete() {
        }
      })
      this.type = type;
      let timer = setTimeout(() => {
        self.setVoiceImg(type, false);
        clearTimeout(timer);
        timer = null;
      }, 3000);
    },
    onUkPlay() {
      let uk = typeMap.uk;
      this.playAudio(uk);
    },
    onUsPlay() {
      let us = typeMap.us;
      this.playAudio(us);
    },
    setVoiceImg(type, active) {
      let obj = {};
      obj[type + 'Img'] = !!active ? proImgs[1] : proImgs[0];
      this.setData(obj);
    },
    audioContextKey(type) {
      return type + 'AudioContext';
    },
    getTopic() {
      return this.data.topic;
    }
  }
})